package com.ioidea.base.widget

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.KeyboardArrowLeft
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonColors
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.Lifecycle
import com.ioidea.base.PreviewPhoneSize
import com.ioidea.base.R
import com.ioidea.base.theme.AppTheme

/**
 * @author: hemin
 * @date: 2024/3/15
 * @Desc:
 */


@Preview(showBackground = true, device = PreviewPhoneSize.PreviewPhoneSize)
@Composable
fun PreviewToolbar() {
    AppTheme {
      /*  Toolbar(
            title = "License",
            )*/

        Toolbar(title = "License",
            bgColor = MaterialTheme.colorScheme.onPrimary,
            backIconTintColor = MaterialTheme.colorScheme.onPrimaryContainer,
            rightTintColor1 = MaterialTheme.colorScheme.onPrimaryContainer,
            titleTextColor = MaterialTheme.colorScheme.onPrimaryContainer,
            rightIcoRes1=com.ioidea.base.R.mipmap.ico_add,
            onClickRight1 = {
            })
    }
}


/**
 * @param title 标题
 * @param showBackIcon 是否显示返回按钮
 * @param titleIsShowCenter 标题是否居中
 * @param bgColor 背景颜色
 * @param titleTextColor 标题颜色
 * @param onClickBack 返回按钮点击事件
 */
@Composable
fun Toolbar(
    title: String,
    showBackIcon: Boolean = true,
    showRightIcon1: Boolean = true,
    showRightIcon2: Boolean = false,
    titleIsShowCenter: Boolean = true,
    rightIcoRes1:Int=R.mipmap.ico_add,
    rightTintColor1:Color=MaterialTheme.colorScheme.onPrimary,
    rightIcoRes2:Int=R.mipmap.ico_add,
    bgColor: Color = MaterialTheme.colorScheme.primary,
    titleTextColor: Color = MaterialTheme.colorScheme.onPrimary,
    backIconTintColor: Color = MaterialTheme.colorScheme.onPrimary,
    onClickBack: () -> Unit = {},
    onClickRight1:()->Unit={},
    onClickRight2:()->Unit={},
) {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .background(bgColor)
    ) {

        if (showBackIcon) {
            TextButton(
                onClick = onClickBack,
                modifier = Modifier.align(Alignment.CenterStart),
            ) {
                Row(verticalAlignment = Alignment.CenterVertically) {
                    Icon(
                        imageVector = Icons.Default.KeyboardArrowLeft,
                        contentDescription = null,
                        tint =backIconTintColor
                    )

                }
            }
        }

            Row(     modifier = Modifier.align(Alignment.CenterEnd)) {
                if (showRightIcon2) {
                    TextButton(
                        onClick = onClickRight2,
//                    modifier = Modifier.background(color = Color.Black)
                    ) {
                        Row(verticalAlignment = Alignment.CenterVertically) {
                            //  R.mipmap.ico_add
                            //R.drawable.ico_search
                            Icon(
                                painterResource(id = rightIcoRes2),
                                modifier = Modifier.size(ButtonDefaults.IconSize),
                                contentDescription = null,
                                tint = MaterialTheme.colorScheme.onPrimary
                            )

                        }
                    }
                }
                if (showRightIcon1){
                    TextButton(
                        onClick = onClickRight1,
                    ) {
                        Row(verticalAlignment = Alignment.CenterVertically) {
                            //  R.mipmap.ico_add
                            //R.drawable.ico_search
                            Icon(
                                painterResource(id =  rightIcoRes1),
                                modifier = Modifier.size(ButtonDefaults.IconSize),
                                contentDescription = null,
                                tint = rightTintColor1
                            )

                        }
                    }
                }

            }


        Text(
            text = title,
            modifier = Modifier
                .padding(12.dp)
                .align(if (titleIsShowCenter) Alignment.Center else Alignment.TopStart),
            color = titleTextColor,
        )
    }
}

@Composable
fun BackArrow(onClickBack: () -> Unit = {}) {
    val lifecycleOwner = LocalLifecycleOwner.current
    TextButton(
        onClick = {
            if (lifecycleOwner.lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)) {
                onClickBack()
            }
        },
    ) {
        Row(verticalAlignment = Alignment.CenterVertically) {
            Icon(
                imageVector = Icons.Default.KeyboardArrowLeft,
                contentDescription = null,
                tint = MaterialTheme.colorScheme.onPrimaryContainer,
                modifier = Modifier.size(ButtonDefaults.IconSize * 2)
            )

        }
    }
}

@Preview(showBackground = true, device = PreviewPhoneSize.PreviewPhoneSize)
@Composable()
fun PreviewCommonButton(modifier: Modifier = Modifier,onClick:()->Unit={}) {

}

@Composable
fun CommonButton(
                 btnText:String="登录",
                 horizontal: Dp = 25.dp,
                 height: Dp = 47.dp,
                 onClick:()->Unit={}) {
    Button(
        onClick = {
            onClick()},
        modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = horizontal)
            .height(height)
    ) {
        Text(
            text = btnText,
            color = MaterialTheme.colorScheme.onPrimary,
            fontSize = 18.sp
        )
    }
}

@Composable
fun CommonSizeButton(modifier: Modifier = Modifier,
                 btnText:String="登录",
                 width: Dp = 100.dp,
                 height: Dp = 40.dp,
                     bgColor: Color = MaterialTheme.colorScheme.primary,
                 onClick:()->Unit={},
                     textSize:TextUnit=18.sp) {
    Button(
        onClick = {
            onClick()},
        modifier = modifier
            .width(width)
            .height(height).background(bgColor, shape = MaterialTheme.shapes.medium),
        colors = ButtonColors(
            containerColor = bgColor,
            contentColor = bgColor,
            disabledContentColor = MaterialTheme.colorScheme.onPrimary.copy(alpha = 0.5f),
            disabledContainerColor = bgColor.copy(alpha = 0.5f),
        )
    ) {
        Text(
            text = btnText,
            color = MaterialTheme.colorScheme.onPrimary,
            fontSize = textSize
        )
    }
}






